package controllers

import (
	"NewEdu/utils"
	"context"
	"time"

	beego "github.com/beego/beego/v2/server/web"
)

// HealthController 健康检查控制器
type HealthController struct {
	beego.Controller
}

// HealthStatus 健康状态结构
type HealthStatus struct {
	Status    string            `json:"status"`
	Timestamp string            `json:"timestamp"`
	Version   string            `json:"version"`
	Services  map[string]string `json:"services"`
}

// Get 健康检查接口
// @Title 健康检查
// @Description 系统健康状态检查
// @Success 200 {object} HealthStatus
// @router / [get]
func (c *HealthController) Get() {
	status := &HealthStatus{
		Status:    "healthy",
		Timestamp: time.Now().Format("2006-01-02 15:04:05"),
		Version:   "1.0.0",
		Services:  make(map[string]string),
	}

	// 检查Redis连接
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()

	if err := utils.RedisClient.Ping(ctx).Err(); err != nil {
		status.Services["redis"] = "unhealthy: " + err.Error()
		status.Status = "degraded"
	} else {
		status.Services["redis"] = "healthy"
	}

	// 这里可以添加更多服务检查，如数据库连接等
	status.Services["database"] = "healthy" // 简化处理

	c.Data["json"] = utils.SuccessWithData(status)
	c.ServeJSON()
}
